package com.common.tools.log;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.common.tools.util.FileUtil;

public class Log {
	
	private final static boolean loggable = true;
	private final static boolean writeFile = false;
	
	public static String SDCARD = "/sdcard/CommonTools";
	
	public static void v(String tag, String msg){
		if(loggable){
			android.util.Log.v(tag, msg);
			toFiletoFile(tag, msg);
		}
	}
	
	public static void d(String tag, String msg){
		if(loggable){
			android.util.Log.d(tag, msg);
			toFiletoFile(tag, msg);
		}
	}
	
	public static void i(String tag, String msg){
		if(loggable){
			android.util.Log.i(tag, msg);
			toFiletoFile(tag, msg);
		}
	}
	
	public static void e(String tag, String msg){
		if(loggable){
			android.util.Log.e(tag, msg);
			toFiletoFile(tag, msg);
		}
	}
	
	public static void e(String tag, String msg, Exception e){
		if(loggable){
			android.util.Log.e(tag, msg, e);
			toFiletoFile(tag, msg);
		}
	}
	
	public static void w(String tag, String msg){
		if(loggable){
			android.util.Log.w(tag, msg);
			toFiletoFile(tag, msg);
		}
	}
	
	public static void toFiletoFile(String tag,String msg){
		String fileName = "log.txt";
		toFiletoFile(tag, fileName, msg);
	}
	
	public static void toFiletoFile(String tag,String fileName,String msg){
		try {
			if(loggable && writeFile){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				msg = "[" + sdf.format(new Date()) + " - " + tag + "]" +  msg + "\n";
				
				FileUtil.writeFileEnd(SDCARD + "/" + fileName, msg);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
